package com.sky.service.impl;


import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.context.BaseContext;
import com.sky.dto.CategoryDTO;
import com.sky.dto.CategoryPageQueryDTO;
import com.sky.entity.Category;
import com.sky.mapper.CategoryMapper;
import com.sky.result.PageResult;
import com.sky.service.CategoryService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
@Slf4j
public class CategoryServiceImpl implements CategoryService {
    @Autowired
    private CategoryMapper categoryMapper;
  //菜品套餐新增
  @Override
  public void addCategory(Category category) {
    LocalDateTime now = LocalDateTime.now();
    category.setCreateTime(now);
    category.setUpdateTime(now);
    category.setCreateUser(BaseContext.getCurrentId());
    category.setUpdateUser(BaseContext.getCurrentId());
    categoryMapper.addCategory(category);
  }
  //菜品分页查询
  @Override
  public PageResult selectCategory(CategoryPageQueryDTO categoryPageQueryDTO) {
    PageHelper.startPage(categoryPageQueryDTO.getPage(),categoryPageQueryDTO.getPageSize());
    Page<Category> page = categoryMapper.selectCategory(categoryPageQueryDTO);
    List<Category> result = page.getResult();
    long total = page.getTotal();
    return new PageResult(total,result);
  }
  //根据分类id删除
  @Override
  public void delectCategory(Integer id) {
    categoryMapper.delectCategory(id);
  }
  //根据id查询数据回显
  @Override
  public List<Category> selectById(Integer id) {
    List<Category> categories = categoryMapper.selectById(id);
    return categories;
  }
  //修改分类根据id
  @Override
  public void putById(Category category) {
    category.setUpdateTime(LocalDateTime.now());
    category.setUpdateUser(BaseContext.getCurrentId());
    categoryMapper.putById(category);
  }
  //根据类型查询分类
  @Override
  public List<Category> selectType(Category category) {
    List<Category> categories = categoryMapper.selectType(category);
    return categories;
  }


}
